﻿#include"infantry/Inc/infantry.h"
#include<QDebug>
#include "UI/quicreator.h"
Infantry::Infantry()
{
    _tTime.update();
    _task = Serial::AUTO_SHOOT;
    _shootTask = Serial::BUFF_SHOOT;
}

void Infantry::Sense()
{
    while(1)
    {
        unsigned char buff[7];
        fd_set fdRead;
        FD_ZERO(&fdRead);
        FD_SET(_serial.getFd(),&fdRead);
        int fdReturn = select(_serial.getFd()+1,&fdRead,0,0,nullptr);
        if(fdReturn <0)
        {
            qDebug() << "select 失败"<<endl;
            continue;
        }
        if(FD_ISSET(_serial.getFd(),&fdRead))
        {
            bool is_read=_serial.ReadData(buff,7);
            if(is_read==false)
            {
                qDebug()  << "读取串口失败" << endl;
                continue;
            }
            if(buff[0] != 's' || buff[6] != 'e')
            {
                qDebug() << "接受数据错误" <<endl;
                continue;
            }
            int mode = (int)buff[1];
            _mutex.lock();
            switch (mode)
            {
                case 1:
                {
                    _task = Serial::AUTO_SHOOT;
                    _shootTask = Serial::BUFF_SHOOT;
                    break;
                }
                case 2:
                {
                    _task = Serial::AUTO_SHOOT;
                    _shootTask = Serial::ARMOR_SHOOT;
                    break;
                }
                case 3:
                {
                     _task = Serial::NO_TASK;
                    _shootTask = Serial::NO_SHOOT;
                    break;
                }
            }
            _mutex.unlock();
        }
    }
}

void Infantry::Consume() 
{
    while (1)
    {
        double time = _tTime.getElapsedTimeInMilliSec();
        qDebug() << time << endl;
        _tTime.update();

        sem_wait(&sem_pro);
        try{
                _buffer.GetImage(_src);
        }catch(...){ 
                qDebug() << "读取相机图片出错" << endl;
                exit(-1);
        }
        sem_post(&sem_com);
//        _mutex.lock();
//        try{
//                _bufferBuff.GetImage(_src2);
//        }catch(...){
//                qDebug() << "读取相机图片出错" << endl;
//                exit(-1);
//        }
//        _mutex.unlock();
        switch (_task)
        {
            case  Serial::AUTO_SHOOT:
                putText( _src, "AUTO_SHOOT", Point(10,30),
		            FONT_HERSHEY_SIMPLEX,0.5, Scalar (0,255,0),2);
                    switch(_shootTask)
                    {
                        case Serial::ARMOR_SHOOT:
                            putText( _src, "ARMOR_SHOOT", Point(10,50),
		                        FONT_HERSHEY_SIMPLEX,0.5, Scalar (80,150,80),2);
                            break;
                        case Serial::BUFF_SHOOT:
                            putText( _src, "BUFF_SHOOT", Point(10,50),
                                    FONT_HERSHEY_SIMPLEX,0.5, Scalar (80,150,80),2);
                            break;
                    }
                    break;
            case Serial::NO_SHOOT :
                putText( _src, "NO_TASK", Point(10,30),
		            FONT_HERSHEY_SIMPLEX,0.5, Scalar (255,0,0),2);
                    break;
        }

        if (_src.size().width != MAT_WIDTH || _src.size().height != MAT_HEIGHT)
        {
            //cv::waitKey(1000);
            continue;
        }
        if(!_src.empty() && !_src2.empty())
        {
            _workState->_shootTask = _shootTask;
            _workState->_task = _task;
            emit getImage(_src2); // up
            if(!_src2.empty())
            {
               emit getImageBottom(_src); //bottom
            }
            emit waitkey(20);
            _workState->startProgram(_serial,_src,_src2);
        }
    }
}

shared_ptr<Phoenix>  InfantryFactory::creatPhoenix()
{
    return shared_ptr<Infantry>(new Infantry());
}

